﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebSite.Models.DepartmentModel>" %>
<%@ Import Namespace="WebSite.Models" %>
<%@ Import Namespace="WebSite.App_Code" %>
<%@ Import Namespace="WebSite.App_Data" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Department
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <style type="text/css">
    
    .DepatmentList
    {
         width:200px;
         border:1px solid black;
    }
    
    .DepatmentList .gray
    {
         background-color:Gray;
         color:White;
    }
    
    </style>

    <script type="text/javascript">

        $(document).ready(function () {

            if ('<%=ViewData.ContainsKey("Select") %>' == 'True') {
                var sel = $('option[value=<%=ViewData["Select"] %>]', $("[name=List]"));
                if (sel.length > 0) { sel.attr("selected", "selected"); }
            }
            else if ($("[name=List] > option").length > 0) { $("[name=List] > option:eq(0)").attr("selected", "selected"); }

            $("[name=List]").focus();
            SelectDepartment($("[name=List]"));

            if ('<%=ViewData.ContainsKey("DB.Operation.Result") %>' == "True")
                alert('<%=ViewData["DB.Operation.Result"] %>');
        });

        function GetDepartmentByLv(sender) {

            var parent_sel = $("select", $(sender).parents("td:eq(0)").next());

            $("option", parent_sel).remove();

            $.getJSON("/MasterData/Department_GetDepartmentByLv", { lv: $(sender).val() }, function (json) {
                $.each(json, function (i, item) {
                    parent_sel.append("<option value='" + item.Value + "'>" + item.Text + "</option>");
                });
            });

        }

        function SelectDepartment(sender) {
            $("input[name=UD_Name]").val($("option:selected",$(sender)).text());
            $("input[name=D_Name]").val($("option:selected", $(sender)).text());
            $("input[name=UD_ID]").val($("option:selected", $(sender)).val());
            $("input[name=DD_ID]").val($("option:selected", $(sender)).val());
            $("input[name=ED_ID]").val($("option:selected", $(sender)).val());
            $("input[name=Up_D_ID]").val($("option:selected", $(sender)).val());
            $("input[name=Dw_D_ID]").val($("option:selected", $(sender)).val());
            $("#dvDetail").show();
            $("#dvUpdate").hide();
            $("#dvInsert").hide();
        }

        function OpenInset() {
            $("#dvDetail").hide();
            $("#dvUpdate").hide();
            $("#dvInsert").show();
        }

        function OpenEdit() {
            if ($("input[name=UD_ID]").val() == "") { alert("请选择组织");return; }

            $("#dvDetail").hide();
            $("#dvUpdate").show();
            $("#dvInsert").hide();
        }

        function DoDisable() { if (confirm("是否删除该组织?")) {  $("#btnDisable").click(); } }

        function DoEnable() { $("#btnEnable").click(); }

        function DoQuery(sender) {

            if (sender.checked) { $("[name=IsAll]").val("True") }
            $("#btnQuery").click();
        }

        function MD_TAB_OpenEdit(btn) {

            $(".masterdatatable tr[RowType=Display]").show();
            $(".masterdatatable tr[RowType=Update]").hide();

            var trDisplay = $(btn).parents("tr:eq(1)");
            var trEdit = trDisplay.nextAll("tr:eq(0)");

            trDisplay.hide();
            trEdit.show();
        }

    </script>
    显示无效组织<input type="checkbox" onclick="DoQuery(this)" <%if(Model.IsAll){%> checked="checked" <%} %> />
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td rowspan="1">
                <select class="DepatmentList" name="List" onclick = "SelectDepartment(this)" size="10">
                    <% foreach (md_Department D in Model.DeptList) { %>
                        <%if (D.Status) { %><option value="<%=D.Guid %>"><%=D.DepartmentName%></option><% } %>
                        <% else { %><option class="gray" value="<%=D.Guid %>"><%=D.DepartmentName%></option><% } %>
                    <% } %>
                </select>            
            </td>
            <td style=" vertical-align:middle">
                <%using (Html.BeginForm("Department_Up", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post))
                  { %>
                    <input name="Up_D_ID" type="hidden" />
                    <input type="submit" class="budget-button-no-01" value="▲" />
                <%} %>
                <%using (Html.BeginForm("Department_Dw", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post))
                  { %>
                    <input name="Dw_D_ID" type="hidden" />
                    <input type="submit" class="budget-button-no-01" value="▼" />
                <%} %>
            </td>
        </tr>
    </table>
    <br/><br/>
    <input type="button" class="budget-button-no-01" value="添加" style=" width:80px;" onclick="OpenInset()" />
    <input type="button" class="budget-button-no-01" value="修改" style=" width:80px;" onclick="OpenEdit()" />
    <input type="button" class="budget-button-no-01" value="删除" style=" width:80px;" onclick="DoDisable()" />
    <input type="button" class="budget-button-no-01" value="启动" style=" width:80px;" onclick="DoEnable()" />
    <div id="dvInsert" style="display:none">
    <%using (Html.BeginForm("Department_Insert", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post)) { %>
        <br />组织名称<br/>
        <input name="ID_Name" class="budget-txt-no-01" style=" width:200px;" />
        <input type="submit" class="budget-button-no-01" value="确定" style=" width:80px;" />
    <%} %>
    </div>
    <div id="dvUpdate" style="display:none">
    <%using (Html.BeginForm("Department_Upadte", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post))
      { %>
        <br />组织名称<br/>
        <input name="UD_Name" class="budget-txt-no-01" style=" width:200px;" />
        <input type="submit" class="budget-button-no-01" value="确定" style=" width:80px;" />
        <input name="UD_ID" type="hidden" />
    <%} %>
    </div>
    <div id="dvDetail" style="display:none">
        <br />组织名称<br/>
        <input name="D_Name" class="budget-txt-no-01" style=" width:200px;" disabled="true" />
    </div>
    <div id="dvDelete" style="display:none">
    <%using (Html.BeginForm("Department_Disable", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post))
      { %>
        <input id="btnDisable" type="submit" class="budget-button-no-01" value="确定" style=" width:80px;" />
        <input name="DD_ID" type="hidden" />
    <%} %>
    </div>
    <div id="dvEnable" style="display:none">
    <%using (Html.BeginForm("Department_Enable", "MasterData", new { ShowAll = Model.IsAll }, FormMethod.Post))
      { %>
        <input id="btnEnable" type="submit" class="budget-button-no-01" value="确定" style=" width:80px;" />
        <input name="ED_ID" type="hidden" />
    <%} %>
    </div>
    <div style="display:none">
    <%using (Html.BeginForm("Department_Query", "MasterData", FormMethod.Post)) { %>
        <input id="btnQuery" type="submit" class="budget-button-no-01" value="确定" style=" width:80px;" />
        <input name="IsAll" type="hidden" />
    <%} %>
    </div>
    <%--<table border="0" cellpadding="0" cellspacing="1" class="masterdatatable">
        <tr class="masterdatatable_header">
            <td>组织名</td>
            <td>组织层级</td>
            <td>上级组织</td>
            <td></td>
        </tr>
            
    <% for (int i = 0; i < Model.Table.Count; i++) { %>
        <% if ((i % 2) == 1) { %> 
            <tr class="masterdatatable_odd" RowType="Display"> 
        <% } else { %> 
            <tr class="masterdatatable_even" RowType="Display">  
        <% } %>
            <td>
                <%=Model.Table[i].Department.DepartmentLevel %>
                <%=Model.Table[i].Department.DepartmentName %>
            </td>
            <td><%=Model.Table[i].Level.DepartmentLevelName %></td>
            <td><%=Model.Table[i].ParentDepartment.DepartmentName %></td>
            <td>
                <a class="linkbutton" onclick="MD_TAB_OpenEdit(this);">Edit</a>
                <%=Html.ActionLink("Delete", "Department_Delete", new { ID = Model.Table[i].Department.Guid }) %>
            </td>
        </tr>
        <% Html.BeginForm("Department_Upadte", "MasterData", FormMethod.Post); %>
            <% if ((i % 2) == 1)
               { %> 
                <tr class="masterdatatable_odd" style="display:none" RowType="Update"> 
            <% }
               else
               { %> 
                <tr class="masterdatatable_even" style="display:none" RowType="Update">  
            <% } %>
                <td>
                    <%=Html.TextBox("Name", Model.Table[i].Department.DepartmentName)%>
                    <%=Html.Hidden("ID",Model.Table[i].Department.Guid) %>
                </td>
                <td><%=Html.DisplayFor(o => o.Table[i].Level.DepartmentLevelName) %></td>
                <td><%=Html.DisplayFor(o => o.Table[i].ParentDepartment.DepartmentName)%></td>
                <td><input type="submit" value="Update" /></td>
            </tr>
        <% Html.EndForm(); %>
    <% } %>--%>
    <%--<%using (Html.BeginForm("Department_Insert", "MasterData", FormMethod.Post)) { %>
        <tr class="masterdatatable_insert"> 
            <td><%=Html.TextBox("Name", Model.Insert.Department.DepartmentName)%></td>
            <td><%=Html.DropDownList("Lv", Model.Insert.LevelSource, new { onchange = "javascript:GetDepartmentByLv(this)" })%></td>
            <td><%=Html.DropDownList("ParentID", Model.Insert.ParentDepartmentSource )%></td>
            <td><input type="submit" value="Insert" /></td>
        </tr>
    <% } %>--%>
    </table>
</asp:Content>
